using Cysharp.Threading.Tasks;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using IQIGame.Onigao.GameAOT;
using UnityEngine;

namespace IQIGame.Onigao.GamePlay
{
    public class UIN_LevelMapTeleportPanel : UIBaseNode
    {
        #region Template Generate,don't modify

        protected class UIB_UIN_LevelMapTeleportPanel
        {
            #region ObjectBinding Generate

            public ExText name { protected set; get; }
            public ExText desc { protected set; get; }
            public ExButton btnTeleport { protected set; get; }
            public ExButton btnTrace { protected set; get; }

            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<ExText>("name", out var __tbv0);
                name = __tbv0;
                __binding.TryGetVariableValue<ExText>("desc", out var __tbv1);
                desc = __tbv1;
                __binding.TryGetVariableValue<ExButton>("btnTeleport", out var __tbv2);
                btnTeleport = __tbv2;
                __binding.TryGetVariableValue<ExButton>("btnTrace", out var __tbv3);
                btnTrace = __tbv3;
            }

            #endregion ObjectBinding Generate
        }

        #endregion Template Generate,don't modify


        #region fields

        private int _entityId;

        private int _entityMcId;

        private int _targetRegionCid;

        #endregion fields

        #region properties

        protected UIB_UIN_LevelMapTeleportPanel ui { get; set; }
        protected UIMsgDispatcher uiMsgDispatcher => _msgDispatcher as UIMsgDispatcher;

        #endregion properties

        protected override void BeforeInit()
        {
            ui = new UIB_UIN_LevelMapTeleportPanel();
            ui.InitBinding(csObjBind);
        }

        protected override void OnInit()
        {
            ui.btnTeleport.AddClickEvent(OnBtnTeleportClick);
            ui.btnTrace.AddClickEvent(OnBtnTraceClick);
        }

        protected override void OnShow()
        {

        }

        protected override void OnHide()
        {

        }

        protected override void OnDispose()
        {

        }


        public void Refresh(IMapSymbolData data, int targetRegionCid)
        {
            _entityId = data.entityId;
            _entityMcId = data.mcid;
            _targetRegionCid = targetRegionCid;
            ui.btnTeleport.SetActive(data.isCheckPointActive);
            ui.btnTrace.SetActive(!data.isCheckPointActive);
            ui.name.text = data.config.Name;
            ui.desc.text = data.config.Describe;
        }

        #region methonds

        private void OnBtnTraceClick()
        {
            if (belongWindow is not UILevelMap uiLevelMap) return;
            uiLevelMap.DoTraceEntity(_entityId).Forget();
        }


        private void OnBtnTeleportClick()
        {
            NoticeModule.ShowNotice(APIUIConst.NoticeCid_Teleport, OnConfirmTeleport);
        }

        private void OnConfirmTeleport()
        {
            if (!FunctionModule.CheckIsOpenAndTip(FunctionType.MapTransmit))
                return;
            TeleportImpl().Forget();
        }

        private async UniTaskVoid TeleportImpl()
        {
            var regionCfg = TableCenter.regionInstance.Get(_targetRegionCid);
            if (regionCfg == null)
            {
                Debug.LogError($"Can't find regionInstance config for {_targetRegionCid}");
                return;
            }

            GameScenePlayUtil.TeleportInGame(_targetRegionCid, _entityMcId).Forget();
            ManagerCenter.UI.CloseWindow<UILevelMap>();
        }

        #endregion methonds
    }
}